package org.example.trainTicketSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.trainTicketSystem.DTO.GetTicketDTO;
import org.example.trainTicketSystem.entity.Ticket;
import org.example.trainTicketSystem.mapper.TicketMapper;
import org.example.trainTicketSystem.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {

    @Autowired
    private TicketMapper ticketMapper;

    @Override
    public Page<Ticket> getTicketsBy(GetTicketDTO getTicketDTO,int page,int size) {
        Page<Ticket> ticketPage = new Page<>(page, size);
        QueryWrapper<Ticket> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_id", getTicketDTO.getTrainId())
                .eq(StringUtils.isNotBlank(getTicketDTO.getSeatType()),"seat_type", getTicketDTO.getSeatType())
                .eq(StringUtils.isNotBlank(getTicketDTO.getStatus()),"status", getTicketDTO.getStatus());
        return ticketMapper.selectPage(ticketPage,queryWrapper);
    }

    @Override
    public int deleteTicket(int trainId) {
        QueryWrapper<Ticket> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_id", trainId);
        return ticketMapper.delete(queryWrapper);
    }

}
